﻿Public Class frmlogin
    Dim Strsql As String
    Dim _Mysql As New ClsSQLhelper
    Public StrCurrGUID As String

    Function Load_ComboboxData_site() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_site = False

            Strsql = "select * from site"
            '   Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = _mysql.GetMYSQLDataset(Strsql, "DS_site", "site")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSite
                If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "site"
                .ValueMember = "siteid"
                .SelectionStart = 0
                If My.Settings.CurrentSite <> "" Then
                    .SelectedValue = My.Settings.CurrentSite
                End If
            End With
            Load_ComboboxData_site = True
        Catch ex As Exception
            ' Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_site - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
            MsgBox("ขออภัย ไม่สามารถติดต่อฐานข้อมูลได้", MsgBoxStyle.Critical, "กรุณาตรวจสอบการตั้งค่า")
            frmDBSetting.ShowDialog()


        End Try
    End Function

    Private Sub BtnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnect.Click
        frmDBSetting.ShowDialog()
    End Sub

    Private Sub frmlogin1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            ' EventLog.Delete("SQL Trace")
            If Not EventLog.SourceExists(Application.ProductName) Then
                EventLog.CreateEventSource(Application.ProductName, "WHIS")
            Else
                '  EventLog1.Clear()
            End If
            'Copy_ApplicationFileUpdate()
            Load_ComboboxData_site()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    'Function Copy_ApplicationFileUpdate() As Boolean
    '    Try
    '        Dim admin_token As IntPtr
    '        Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent()
    '        Dim wid_admin As WindowsIdentity = Nothing
    '        Dim wic As WindowsImpersonationContext = Nothing

    '        Dim MyFileDirectory As New DirectoryInfo(Application.StartupPath & "\report")
    '        If MyFileDirectory.Exists = False Then MyFileDirectory.Create()

    '        If LogonUser("administrator", "192.168.1.1", "sys11266", 9, 0, admin_token) <> 0 Then
    '            wid_admin = New WindowsIdentity(admin_token)
    '            wic = wid_admin.Impersonate()
    '            My.Computer.FileSystem.CopyDirectory("\\192.168.1.1\whis\Report", Application.StartupPath & "\report", True)
    '            'File.Copy("\\192.168.1.1\whis\Report\RptProrder.rpt", Application.StartupPath & "\report\RptProrder.rpt", True)
    '            'File.Copy("\\192.168.1.1\whis\Report\RptLabOrder.rpt", Application.StartupPath & "\report\RptLabOrder.rpt", True)
    '        Else
    '            MsgBox("Cannot copy report files")
    '        End If

    '    Catch ex As Exception
    '        MsgBox(ex.Message)
    '    End Try

    'End Function

    Private Sub TxtPassword_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtPassword.KeyDown
        If e.KeyCode = Keys.Enter Then
            BtnOK_Click(sender, e)
        End If
    End Sub

    Private Sub TxtPassword_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtPassword.TextChanged

    End Sub

    Private Sub TxtUser_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtUser.KeyDown
        If e.KeyCode = Keys.Enter Then
            BtnOK_Click(sender, e)
        End If
    End Sub

    Private Sub TxtUser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtUser.TextChanged

    End Sub

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        Try
            If Me.TxtUser.Text.Trim = "" Then
                MsgBox("กรุณากรอก Username", MsgBoxStyle.Critical)
                Me.TxtUser.Focus()
                Exit Sub
            End If
            If Me.TxtPassword.Text.Trim = "" Then
                MsgBox("กรุณากรอก Password", MsgBoxStyle.Critical)
                Me.TxtPassword.Focus()
                Exit Sub
            End If
            Dim DS As New DataSet
            Strsql = "Select * from user left join siteuser on user.user = siteuser.user where user.user = '" & Me.TxtUser.Text.Trim & "' and user.password  = '" & Crypto.Crypto.Encrypt(TxtPassword.Text, "sys11266") & "' and siteuser.siteid = " & Me.CboSite.SelectedValue
            '  Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = _Mysql.GetMYSQLDataset(Strsql, "DS_user", "user")
            If DS.Tables(0).Rows.Count < 1 Then
                MsgBox("Username หรือ Password ไม่ถูกต้อง", MsgBoxStyle.Critical)
                Exit Sub
            Else

                ' frmmain.User = Me.TxtUser.Text
                'frmmain.TxtUser.Text = DS.Tables(0).Rows(0).Item("user_name")
                'frmmain.TxtSiteName.Text = "คลังวัสดุ : " & CboSite.Text.ToString
                'frmmain.TxtComputername.Text = "Computer : " & Pn_Framework.Get_Computername
                'My.Settings.CurrentuserGroup = DS.Tables(0).Rows(0).Item("usergroup")


                Strsql = "Select * from onlineuser where computername = '" & Pn_Framework.Get_Computername & "'"
                ' Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = _Mysql.GetMYSQLDataset(Strsql, "DS_onlineuser", "onlineuser")
                If DS.Tables(0).Rows.Count > 0 Then
                    Strsql = "Delete from onlineuser where computername = '" & Pn_Framework.Get_Computername & "'"
                    _Mysql.MySQLExecute(Strsql)
                End If
                StrCurrGUID = Pn_Framework.Get_GUID
                Strsql = "Insert into onlineuser (user,computername,ipaddress,site,onlineid,os,whisversion,onlinedatetime) values ('" & Me.TxtUser.Text.Trim & "','" & Pn_Framework.Get_Computername & "','" & Pn_Framework.Get_IPAddress & "','" & Me.CboSite.SelectedValue & "','" & StrCurrGUID & "','" & Pn_Framework.Get_OS & "','" & My.Application.Info.Version.ToString & "','" & Pn_Framework.MysqlDateTimeFormat(Now) & "')"
                _Mysql.MySQLExecute(Strsql)
            End If



            frmmain.Show()
        Catch ex As Exception
            ' Me.EventLog1.WriteEntry(Me.Name & " : Function Login - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub
End Class